package com.cmc.cloud.cmclink.doc.controller.admin.so;

import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.convert.BasicConfigConvert;
import com.cmc.cloud.cmclink.doc.entity.BasicConfigDO;
import com.cmc.cloud.cmclink.doc.service.so.BasicConfigService;
import com.cmc.cloud.cmclink.doc.vo.sovo.basicconfgvo.*;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

/**
 * 用户自定义信息前端接口Controller
 *
 * @author codegen
 */
@Tag(name = "管理后台 - 过境提单信息维护")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/basic-config")
public class BasicConfigController {

    @Resource
    private BasicConfigService basicConfigService;


    /**
     * 校验规则不一样所以日本海关申报操作人代码
     *
     * @param createReqVO
     * @return
     */
    @Operation(summary = "创建日本海关申报操作人代码")
    @PostMapping("/jp/create")
    @PreAuthorize("@ss.hasPermission('doc:customsDeclarationOperator:add')")
    public CommonResult<Void> createJpBasicConfig(@Valid @RequestBody BasicConfigJpBaseVO createReqVO) {
        basicConfigService.createJpBasicConfig(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新日本海关申报操作人代码")
    @PostMapping("/jp/update")
    @PreAuthorize("@ss.hasPermission({'doc:customsDeclarationOperator:enable','doc:customsDeclarationOperator:edit'})")
    public CommonResult<Void> updateBasicConfig(@Valid @RequestBody BasicConfigJpUpdateReqVO updateReqVO) {
        basicConfigService.updteJpBasicConfig(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "创建用户自定义信息")
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermission('doc:transitInfoMaintain:add')")
    public CommonResult<Void> createBasicConfig(@Valid @RequestBody BasicConfigCreateReqVO createReqVO) {
        basicConfigService.createBasicConfig(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新用户自定义信息")
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermission('doc:transitInfoMaintain:edit')")
    public CommonResult<Void> updateBasicConfig(@Valid @RequestBody BasicConfigUpdateReqVO updateReqVO) {
        basicConfigService.updateBasicConfig(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "删除用户自定义信息")
    @PostMapping("/delete/{id}")
    @PreAuthorize("@ss.hasPermission({'doc:transitInfoMaintain:delete','doc:customsDeclarationOperator:delete'})")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Void> deleteBasicConfig(@PathVariable("id") Long id) {
        basicConfigService.deleteBasicConfig(id);
        return CommonResult.success();
    }

    @Operation(summary = "批量删除用户自定义信息")
    @PostMapping("/delete/batch")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('doc:transitInfoMaintain:batchDelete')")
    public CommonResult<Void> deleteBatchBasicConfig(@RequestParam("ids") Collection<Long> ids) {
        basicConfigService.deleteBatchBasicConfig(ids);
        return CommonResult.success();
    }

    @Operation(summary = "获得用户自定义信息")
    @GetMapping("/get/{id}")
    @UseResponsePropertyAdvice
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('doc:transitInfoMaintain:search')")
    public CommonResult<BasicConfigRespVO> getBasicConfig(@PathVariable("id") Long id) {
        BasicConfigDO basicConfig = basicConfigService.getBasicConfig(id);
        return CommonResult.success(BasicConfigConvert.INSTANCE.convert(basicConfig));
    }

    @Operation(summary = "获得用户自定义信息列表")
    @GetMapping("/list")
    @UseResponsePropertyAdvice
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<BasicConfigRespVO>> getBasicConfigList(@RequestParam("ids") Collection<Long> ids) {
        List<BasicConfigDO> list = basicConfigService.getBasicConfigList(ids);
        return CommonResult.success(BasicConfigConvert.INSTANCE.convertList(list));
    }

    @Operation(summary = "获得用户code-define字段")
    @GetMapping("/code-define/list")
    @UseResponsePropertyAdvice
    public CommonResult<List<String>> getBasicConfigCodeDefineList() {
        List<String> list = basicConfigService.getBasicConfigCodeDefineList();
        return CommonResult.success(list);
    }

    @Operation(summary = "获得用户自定义信息分页")
    @PostMapping("/page")
    @UseResponsePropertyAdvice
    @PreAuthorize("@ss.hasPermission('doc:customsDeclarationOperator:search')")
    public CommonResult<PageResult<BasicConfigRespVO>>
    getBasicConfigPage(@Valid @RequestBody BasicConfigPageReqVO pageVO) {
        PageResult<BasicConfigDO> pageResult = basicConfigService.getBasicConfigPage(pageVO);
        List<BasicConfigRespVO> results = BasicConfigConvert.INSTANCE.convertList(pageResult.getList());
        return CommonResult.success(new PageResult<>(results, pageResult.getTotal()));
    }
}
